﻿
using Xacml.Core;
using System;
using System.Xml;

namespace Xacml.Core.Context
{
  public abstract class TargetItemBase : XacmlElement
  {
    private AttributeCollectionReadWrite _attributes = new AttributeCollectionReadWrite();

    public virtual AttributeCollectionReadWrite Attributes
    {
      get
      {
        return this._attributes;
      }
      set
      {
        this._attributes = value;
      }
    }

    public override bool IsReadOnly
    {
      get
      {
        return false;
      }
    }

    private TargetItemBase(XacmlVersion schemaVersion)
      : base(XacmlSchema.Context, schemaVersion)
    {
    }

    protected TargetItemBase(AttributeCollectionReadWrite attributes, XacmlVersion schemaVersion)
      : this(schemaVersion)
    {
      this._attributes = attributes;
    }

    protected TargetItemBase(XmlReader reader, string itemNodeName, XacmlVersion schemaVersion)
      : this(schemaVersion)
    {
      if (reader.LocalName == itemNodeName)
      {
        if (reader.HasAttributes)
        {
          while (reader.MoveToNextAttribute())
            this.AttributeFound(reader.NamespaceURI, reader.LocalName, reader.Value);
          reader.MoveToElement();
        }
        while (reader.Read())
        {
          switch (reader.LocalName)
          {
            case "Attribute":
              this._attributes.Add(new AttributeElementReadWrite(reader, schemaVersion));
              break;
            default:
              this.NodeFound(reader);
              break;
          }
          if (reader.LocalName == itemNodeName && reader.NodeType == XmlNodeType.EndElement)
            break;
        }
      }
      else
        throw new Exception(Resource.ResourceManager[Resource.MessageKey.exc_invalid_node_name, new object[1]
        {
          (object) reader.LocalName
        }]);
    }

    protected abstract void AttributeFound(string namespaceName, string attributeName, string attributeValue);

    protected abstract void NodeFound(XmlReader reader);
  }
}
